import os
import sys
import subprocess

class SikuliRun(object):
    def __init__(self, sikuli_script, sikulix_jar):
        # sikuli_script should be a directory, containing the .py script
        # for test steps, and all the .png files.
        assert os.path.isdir(sikuli_script)
        # sikuli_jar should be a file, with the name of 'sikulix.jar'.
        assert os.path.isfile(sikulix_jar)
        sikulix_jar_file = os.path.basename(sikulix_jar)
        assert sikulix_jar_file == 'sikulix.jar'
        self.sikuli_script = sikuli_script
        self.sikulix_jar = sikulix_jar

    def run_sikuli(self):
        # if all runs well, a return code of 0 is seen. Otherwise, non-zero.
        subprocess.check_call(['java', '-Xmx512M',
            '-Dapple.laf.useScreenMenuBar', '-Dfile.encoding=UTF-8',
            '-Dsikuli.FromCommandLine', '-jar', self.sikulix_jar,
            '-r', self.sikuli_script])
        sys.stdout.write("Don't worry if this is seen on pybot console:")
        sys.stdout.write('Exception in thread "Thread-2"' + 
            'java.lang.NullPointerException')
        sys.stdout.write("Some Java class wrote it and I can't turn it off!")
